package domain

import (
	"github.com/lab-online/internal/class/constant"
	"github.com/lab-online/internal/class/entity"
	"github.com/lab-online/internal/shared/code"
	"github.com/lab-online/pkg/auth"
)

func (d *Domain) CreateClass(
	classEntity entity.ClassEntity,
	authEntity auth.AuthEntity,
) (entity.ClassEntity, error) {
	if !classEntity.CanHandleClass(authEntity, false) {
		return nil, constant.RESTErrNoPermissionToCreateClass
	}

	if _, err := d.userRepository.GetUserByUsername(classEntity.GetTeacher()); err != nil {
		return nil, code.ErrorPicker(err, code.DatabaseUserNotFound, constant.RESTErrTeacherNotExist)
	}

	if _, err := d.courseRepository.GetCourseByID(classEntity.GetCourseID()); err != nil {
		return nil, code.ErrorPicker(err, code.DatabaseCourseNotFound, constant.RESTErrCourseNotExist)
	}

	return d.repository.SaveClass(classEntity)
}
